﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LcAudit.BPM.Core;
using LcAudit.Common;
using System.Data;
using LcAudit.Common.Data;
using LcAudit.Common.Data.Filter;

namespace KaoQin.Web.ajax
{
    public partial class ExportExcel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!SysVisitor.Instance.IsGuest)
            {
                if (Request["mytype"] == null)
                {
                    string fields = PublicMethod.GetString(Request["fields"]);
                    string filters = PublicMethod.GetString(Request["filters"]);
                    string tableName = PublicMethod.GetString(Request["tableName"]);


                    var pcp = new ProcCustomPage(tableName)
                    {
                        ShowFields = fields,
                        PageIndex = 1,
                        PageSize = 9999999,
                        OrderFields = "",
                        WhereString = FilterTranslator.ToSql(filters)
                    };
                    int recordCount;
                    DataTable dt=null;
                    if (!tableName.StartsWith("1"))
                        dt = DbUtils.GetPageWithSp(pcp, out recordCount);
                    else
                        dt = DbUtils.GetPageWithSpPhone(pcp, out recordCount);


                    GridViewExportUtil.Export(DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".xls", dt);

                }
                else if (Request["mytype"]=="js")
                {

            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "utf-8";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AppendHeader("content-disposition", "attachment;filename=\"" + HttpUtility.HtmlEncode(Request["txtName"]??DateTime.Now.ToString("yyyyMMdd")) + ".xls\"");
            Response.ContentType = "Application/ms-excel";
            Response.Write("<html>\n<head>\n");
            Response.Write("<style type=\"text/css\">\n.pb{font-size:13px;border-collapse:collapse;} "+
                           "\n.pb th{font-weight:bold;text-align:center;border:0.5pt solid windowtext;padding:2px;} " +
                           "\n.pb td{border:0.5pt solid windowtext;padding:2px;}\n</style>\n</head>\n");
            Response.Write("<body>\n" + Request["txtContent"] + "\n</body>\n</html>");
            Response.Flush();
            Response.End(); 
       
                }
                
            }
            else
            {
                Response.Write("<h1>没有登录啊，你懂的！</h1>");
            }
        }
    }
}